var express = require('express');
var Promise = require('promise');
var router = express.Router();

/* GET users listing. */
router.get('/all', function(req, res, next){
	new Promise(function(resolve, reject){
		req.VandB.count({name:new RegExp(req.query.keyword,'i'),pass:true},function(err,result){
			if (err) {
			reject(err);
			}
			resolve(result);
		});
	}).then(function(count){
		if(!req.query.page || req.query.page==""){
			var	page = 1;
		} else {
			page = req.query.page;
		}

		skip = (page-1)*16;

		req.VandB.find({name:new RegExp(req.query.keyword,'i'),pass:true}).skip(skip).limit(16).toArray(function(err, items){
				res.render('select',{
					'title':req.query.keyword+'-搜索结果',
					'user':req.infos,
					'VA':items,
					'find':req.query.keyword,
					'router':"/select/all",
					'count':count,
					'page':page,
				})
		});
	});
})

router.get('/article', function(req, res, next){
	new Promise(function(resolve, reject){
		req.VandB.count({name:new RegExp(req.query.keyword,'i'),type:"article",pass:true},function(err,result){
			if (err) {
			reject(err);
			}
			resolve(result);
		});
	}).then(function(count){
		if(!req.query.page || req.query.page==""){
			var	page = 1;
		} else {
			page = req.query.page;
		}

		skip = (page-1)*16;

		req.VandB.find({name:new RegExp(req.query.keyword,'i'),type:"article",pass:true}).skip(skip).limit(16).toArray(function(err, items){
				res.render('select',{
					'title':req.query.keyword+'-搜索结果',
					'user':req.infos,
					'VA':items,
					'find':req.query.keyword,
					'router':"/select/article",
					'count':count,
					'page':page,
				})
		});
	});
})

router.get('/video', function(req, res, next){
new Promise(function(resolve, reject){
		req.VandB.count({name:new RegExp(req.query.keyword,'i'),type:"video",pass:true},function(err,result){
			if (err) {
			reject(err);
			}
			resolve(result);
		});
	}).then(function(count){
		if(!req.query.page || req.query.page==""){
			var	page = 1;
		} else {
			page = req.query.page;
		}

		skip = (page-1)*16;

		req.VandB.find({name:new RegExp(req.query.keyword,'i'),type:"video",pass:true}).skip(skip).limit(16).toArray(function(err, items){
					res.render('select',{
						'title':req.query.keyword+'-搜索结果',
						'user':req.infos,
						'VA':items,
						'find':req.query.keyword,
						'router':"/select/video",
						'count':count,
						'page':page,
					})
			});
	});
})

router.get('/user', function(req, res, next){

	new Promise(function(resolve, reject){
		req.users.count({name:new RegExp(req.query.keyword,'i')},function(err,result){
			if (err) {
			reject(err);
			}
			resolve(result);
		});
	}).then(function(count){
		if(!req.query.page || req.query.page==""){
			var	page = 1;
		} else {
			page = req.query.page;
		}

		skip = (page-1)*16;
		req.users.find({name:new RegExp(req.query.keyword,'i')}).skip(skip).limit(16).toArray(function(err, items){
				res.render('select',{
					'title':req.query.keyword+'-搜索结果',
					'user':req.infos,
					'ups':items,
					'find':req.query.keyword,
					'router':"/select/user",
					'count':count,
					'page':page,
				});
		});
	});
})


module.exports = router;